package com.zuipin.service.impl;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.zuipin.entity.IntegralRecord;
import com.zuipin.entity.Member;
import com.zuipin.entity.MemberAccount;
import com.zuipin.entity.MemberLevel;
import com.zuipin.entity.MemberRecord;
import com.zuipin.enums.IntegralType;
import com.zuipin.enums.MemberRecordType;
import com.zuipin.framework.emuns.ErrorCode;
import com.zuipin.framework.exception.CustomException;
import com.zuipin.framework.result.Result;
import com.zuipin.mapper.MemberRecordMapper;
import com.zuipin.service.IIntegralRecordService;
import com.zuipin.service.IMemberAccountService;
import com.zuipin.service.IMemberRecordService;
import com.zuipin.service.IMemberService;
import com.zuipin.util.ConstantsUtil;
import com.zuipin.util.DateUtils;
import com.zuipin.util.Pagination;

/**
 * @ClassName: MemberRecordServiceImpl
 * @author wuyicheng
 * @time 2017年3月27日下午4:02:46
 */
@Service
public class MemberRecordServiceImpl implements IMemberRecordService {
	@Resource
	private MemberRecordMapper		memberRecordMapper;
	@Resource
	private IMemberService			memberService;
	@Resource
	private IMemberAccountService	memberAccountService;
	@Resource
	private IIntegralRecordService	integralRecordService;
	
	/**
	 * @Title: findById
	 * @author: wuyicheng
	 * @date: 2017年3月27日下午5:25:36
	 * @param id
	 * @return
	 * @throws
	 */
	@Override
	public MemberRecord findById(Long id) {
		return memberRecordMapper.findById(id);
	}
	
	/**
	 * @Title: findList
	 * @author: wuyicheng
	 * @date: 2017年3月27日下午5:25:36
	 * @param memberRecord
	 * @return
	 * @throws
	 */
	@Override
	public List<MemberLevel> findList(MemberRecord memberRecord) {
		return null;
	}
	
	/**
	 * @Title: insert
	 * @author: wuyicheng
	 * @date: 2017年3月27日下午5:25:36
	 * @param memberRecord
	 * @throws
	 */
	@Override
	public void insert(MemberRecord memberRecord) {
		memberRecord.setCreateTime(DateUtils.getCurrentDateTime());
		memberRecord.setUpdateTime(DateUtils.getCurrentDateTime());
		memberRecord.setDelFlag(ConstantsUtil.DEL_FLAG_NO_0);
		memberRecordMapper.insert(memberRecord);
	}
	
	/**
	 * @Title: update
	 * @author: wuyicheng
	 * @date: 2017年3月27日下午5:25:36
	 * @param memberRecord
	 * @throws
	 */
	@Override
	public void update(MemberRecord memberRecord) {
		memberRecord.setUpdateTime(DateUtils.getCurrentDateTime());
		memberRecordMapper.update(memberRecord);
	}
	
	/**
	 * @Title: deleteById
	 * @author: wuyicheng
	 * @date: 2017年3月27日下午5:25:36
	 * @param id
	 * @throws
	 */
	@Override
	public void deleteById(Long id) {
		MemberRecord memberRecord = new MemberRecord();
		memberRecord.setDelFlag(ConstantsUtil.DEL_FLAG_YES_1);
		update(memberRecord);
	}
	
	/** 
	 * 判断是否能签到
	 * 
	 * @Title: isSignInToday 
	 * @author: wuyicheng
	 * @date: 2017年4月25日上午1:12:33 
	 * @param memberId
	 * @param sysId
	 * @return true 已经签到   false 未签到
	 * @throws 
	 */
	@Override
	public boolean isSignInToday(Long memberId, Long sysId) {
		MemberRecord memberRecord = new MemberRecord();
		memberRecord.setMemberId(memberId);
		memberRecord.setSysId(sysId);
		memberRecord.setLogType(MemberRecordType.SIGN_IN);
		memberRecord.setCreateTime(DateUtils.getCurrentDate());
		List<MemberRecord> list = memberRecordMapper.findByDate(memberRecord);
		if (list != null && list.size() > 0) {
			return true;
		}
		return false;
	}
	
	/**
	 * @throws CustomException 
	 * 当天签到
	 * 
	 * @throws Exception 
	 * @return  
	 *
	 * @Title: doSignInToday 
	 * @author: wuyicheng
	 * @date: 2017年4月25日上午2:15:23 
	 * @param memberId
	 * @param sysId
	 * @param type
	 * @param content
	 * @param result
	 * @throws 
	 */
	@Override
	public Result<Map<String, Object>> doSignInToday(Long memberId, Long sysId, MemberRecordType type, String content, Result<Map<String, Object>> result) throws CustomException {
		
		MemberAccount memberAccount = memberAccountService.findByMemberIdAndSysId(memberId, sysId);
		if (memberAccount != null) {
			Member member = memberService.findById(memberId);
			// && StringUtil.isNotBlank(member.getPhone())
			// 不需要判断会员是否绑定手机，前端控制
			if (member != null) {
				boolean flag = isSignInToday(memberId, sysId);
				if (!flag) {
					addMemberRecordLog(memberId, sysId, type, content);
					integralRecordService.addIntegralRecord(memberId, sysId, null, null, 30, IntegralType.SIGN_IN, "签到", "", "", IntegralRecord.IS_BUY_NO);
					result.setData(null);
					result.setErrorCode(ErrorCode.SUCCESS);
				} else {
					result.setData(null);
					result.setErrorCode(ErrorCode.DB_EXCEPTION);
					result.setMessage("Today has been signed in, please come again tomorrow");
				}
			} else {
				result.setData(null);
				result.setErrorCode(ErrorCode.DB_EXCEPTION);
				result.setMessage("member is not exist");
			}
		} else {
			result.setData(null);
			result.setErrorCode(ErrorCode.DB_EXCEPTION);
			result.setMessage("memberAccount is not exist");
		}
		
		return result;
	}
	
	/** 
	 * 添加会员日志
	 * 
	 * @Title: addMemberRecordLog 
	 * @author: wuyicheng
	 * @date: 2017年5月4日下午2:52:44 
	 * @param memberId 会员ID
	 * @param sysId 系统ID
	 * @param type 类型
	 * @param content 备注内容
	 * @return: void
	 * @throws 
	 */
	@Override
	public void addMemberRecordLog(Long memberId, Long sysId, MemberRecordType type, String content) {
		MemberRecord memberRecord = new MemberRecord();
		memberRecord.setLogType(type);
		memberRecord.setMemberId(memberId);
		memberRecord.setSysId(sysId);
		memberRecord.setRemark(content);
		insert(memberRecord);
	}
	
	/** 
	 *
	 * @Title: searchSignInByPage 
	 * @author: wuyicheng
	 * @date: 2017年5月9日下午3:21:56 
	 * @param memberId
	 * @param sysId
	 * @param pagination
	 * @return
	 * @throws 
	 */
	@Override
	public List<MemberRecord> searchSignInByPage(Long memberId, Long sysId, Pagination pagination) {
		MemberRecord memberRecord = new MemberRecord();
		memberRecord.setMemberId(memberId);
		memberRecord.setSysId(sysId);
		return memberRecordMapper.searchSignInByPage(memberRecord, pagination);
	}
	
}
